
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Diagnostics;

using Swaf;
using Swaf.BizRule;

namespace Swaf.DataAccess
{
	public enum TransactionSupport {none, uses, requires, requiresNew};
	public class RuleTransactionDecorator : IRuleDecorator
	{
		protected TransactionSupport m_support = TransactionSupport.uses;
		protected string m_name;

		public RuleTransactionDecorator(string name, string val, object extraInfo)
		{
			m_name = name;
			m_support = (TransactionSupport)Enum.Parse(typeof(TransactionSupport), val, true);
		}

		#region IRuleDecorator Members

		public string Value	{get{return m_support.ToString();}}

		public string Name {get{return m_name;}}

		public object decorateInstance(object theInst)
		{
			return theInst;
		}

		public bool IsStandin {get{return true;}}

		/// <summary>
		/// This call will setup the transaction manager to support the transaction support required by the rule
		/// before the rule is executed.
		/// </summary>
		/// <param name="rule"></param>
		/// <returns>Always returns true because a rule will never not be executed due to transaction issues.</returns>
		public bool preRuleExecution(IRunningRule rule)
		{
			if(m_support != TransactionSupport.none)
			{
				string parentId = string.Empty;
				ITransactionManager t = null;
				if(rule.Parent != null)
				{
					parentId = rule.Parent.UniqueId.ToString();
					t = (ITransactionManager)rule.Parent["_transaction:" + parentId];
				}
				else
				{
					t = (ITransactionManager)rule["_transaction:" + parentId];
				}

				if(m_support == TransactionSupport.requiresNew || (t == null && m_support == TransactionSupport.requires))
				{
					t = new BasicTransaction();
				}

				if (t != null)
				{
					t.addParticipant(rule.UniqueId);
				}

				rule["_transaction:" + rule.UniqueId.ToString()] = t;
			}

			return true;
		}

		/// <summary>
		/// This call will check to see if a transaction is completed based on this
		/// rule being the initial rule that created the transaction
		/// </summary>
		/// <param name="info"></param>
		public void postRuleExecution(IRunningRule rule, ref object ruleReturn, ref Exception ruleException)
		{
			ITransactionManager t = (ITransactionManager)rule["_transaction:" + rule.UniqueId.ToString()];
			if(t != null)
			{
				if(!t.hasIdentityVoted(rule.UniqueId) && ruleException != null)
				{
					((ITransaction)t).voteRollBack();
				}
				t.removeParticipant(rule.UniqueId);
			
				rule["_transaction:" + rule.UniqueId.ToString()] = null;
			}
		}
		#endregion
	}

}